#include "CheckListDataGrid.hpp"
#include "imgui.h"
#include <iostream>
#include <string>
#include "Utils/String.hpp"

using namespace PokerBoxControls;
using namespace PokerBoxUtils;

PokerBoxControls::CheckListDataGrid::CheckListDataGrid()
{
    _reader = new PokerBoxUtils::CsvReader("Hello.txt");
    _checkItems = this->GetCheckItems();
}

CheckListDataGrid::~CheckListDataGrid()
{
    if (_reader != nullptr)
    {
        delete _reader;
    }

    if (_checkItems != nullptr)
    {
        delete _checkItems;
    }
}

vector<CutterCheckItem> *CheckListDataGrid::GetCheckItems()
{
    auto rows = _reader->GetRows();
    auto rowCount = _reader->GetRowCount();
    auto items = new vector<CutterCheckItem>();

    for (size_t rowIndex = 0; rowIndex < rowCount; rowIndex++)
    {
        auto row = rows[rowIndex];
        auto columnCount = row.GetColumnCount();

        if (columnCount > 0 && String::Trim(row.GetCsvItem(0).GetContent()).size() > 0)
        {
            std::string str = row.GetCsvItem(0).GetContent();
            auto newItem = CutterCheckItem(str);
            items->push_back(newItem);
        }
    }

    return items;
}

void CheckListDataGrid::Render()
{
    const int columnCount = 2;
    const ImGuiTableFlags flags = ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit;
    static int selectedIndex = 0;

    if (ImGui::BeginTable("mytable", columnCount, flags))
    {
        ImGui::PushID(0);
        ImGui::TableSetupColumn(u8"点检结果", ImGuiTableColumnFlags_NoResize);
        ImGui::PopID();
        ImGui::PushID(1);
        ImGui::TableSetupColumn(u8"点检项描述", ImGuiTableColumnFlags_WidthStretch);
        ImGui::PopID();
        ImGui::TableHeadersRow();

        for (int rowIndex = 0; rowIndex < _checkItems->size(); rowIndex++)
        {
            ImGui::TableNextRow();
            for (int columnIndex = 0; columnIndex < 2; columnIndex++)
            {
                ImGui::TableNextColumn();

                if (columnIndex == 0)
                {
                    ImGui::PushID(rowIndex);
                    ImGui::RadioButton(u8"未点检", &((*_checkItems)[rowIndex].Status), 0);
                    ImGui::SameLine();
                    ImGui::RadioButton("OK", &((*_checkItems)[rowIndex].Status), 1);
                    ImGui::SameLine();
                    ImGui::RadioButton("NG", &((*_checkItems)[rowIndex].Status), 2);
                    ImGui::SameLine();
                    ImGui::RadioButton(u8"不适用", &((*_checkItems)[rowIndex].Status), 3);
                    ImGui::PopID();
                }
                else
                {
                    ImGui::Text((*_checkItems)[rowIndex].Description.c_str());
                }
            }
        }

        ImGui::EndTable();
    }
}
